package com.qidian.blog.util;

import java.sql.Time;
import java.util.ArrayList;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

/**
 * @author 戴着假发的程序员
 * @company 起点编程
 * 2022/10/12 16:39
 */
public class ThreadStorage {
    private static Timer timer = new Timer();
    private static List<Thread> ts = new ArrayList<>();
    public static void addThread(Thread t){
        ts.add(t);
    }
    static {
        // 准备一个定时任务，可以用来定时清理已经结束的线程对象
        timer.schedule(new TimerTask() {
            @Override
            public void run() {
                for (int x = 0;x < ts.size();x ++){
                    Thread t = ts.get(x);
                    if(!t.isAlive()){
                        ts.remove(t);
                    }
                }
            }
        },1000,1000);
    }
}
